package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.SharedBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.AbDateUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SharedActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.edit_share)
    TextView editShare;
    @Bind(R.id.account_money)
    TextView accountMoney;
    @Bind(R.id.freeze_money)
    TextView freezeMoney;
    @Bind(R.id.money_recorder_list)
    ListView moneyRecorderList;
    @Bind(R.id.notice_refresh_view)
    SwipeRefreshLayout noticeRefreshView;
    private SharedAdapter sharedAdapter;
    private LayoutInflater inflater;
    private List<SharedBean.DatasBean.DataBean> info = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.shared_menubar_inc);
        super.MenuWidget(headView);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        sharedAdapter = new SharedAdapter(this);

        initShared();
        moneyRecorderList.setAdapter(sharedAdapter);
        noticeRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        noticeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                initShared();
            }
        });
    }

    public void initShared() {
        String url = ""+MainActivity.url+"act=goods&op=my_tj&member_id="+ MainActivity.myLogin.getMemberid()+"";
        Log.i("url=====",url);

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(10000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        noticeRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            if (result == 200) {
                                info.clear();
                                JSONObject information = resultObject.optJSONObject("data");
                                Log.i("zhangxiaoxidebug", "information=" + information);

                                JSONArray listArray = information.optJSONArray("data");
                                Log.i("zhangxiaoxidebug", "listArray=" + listArray);
                                if(listArray.length()>=0) {
                                    String total = information.optString("total");
                                    String tjm = information.optString("tjm");
                                    if(total=="null"&&total.equals("null")){
                                        accountMoney.setText("0");
                                    }else{
                                    accountMoney.setText(total);}
                                    if(tjm=="null"&&tjm.equals("null")){
                                        freezeMoney.setText("0");}
                                    else{
                                    freezeMoney.setText(tjm);}
                                    for (int i = 0; i < listArray.length(); i++) {
                                        JSONObject recordObject = listArray.optJSONObject(i);
                                        String member_id = recordObject.optString("member_id");
                                        String member_name = recordObject.optString("member_name");
                                        long member_time = recordObject.optLong("member_time");
                                        int yizhifu_tj_amount = recordObject.optInt("yizhifu_tj_amount");
                                        int complete_tj_amount = recordObject.optInt("complete_tj_amount");


                                        info.add(new SharedBean.DatasBean.DataBean(member_id, member_name, member_time, yizhifu_tj_amount, complete_tj_amount));
                                    }
                                    sharedAdapter.setList(info);
                                    sharedAdapter.notifyDataSetChanged();
                                }
                                else {
                                    String tjm = information.optString("tjm");
                                    freezeMoney.setText(tjm);
                                    accountMoney.setText("0");
                                }
                            } else {
                                toast("当前暂无数据");
                                noticeRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            noticeRefreshView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        noticeRefreshView.setRefreshing(false);
                        toast("请求失败");
                        noticeRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    @OnClick({R.id.go_back, R.id.edit_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.edit_share:
                Intent quickmarkInt = new Intent(SharedActivity.this, ShareDoActivity.class);
                startActivity(quickmarkInt);
                break;
        }
    }

    class SharedAdapter extends BaseAdapter {
        private List<SharedBean.DatasBean.DataBean> info = new ArrayList<SharedBean.DatasBean.DataBean>();
        private Context context;

        public SharedAdapter(Context context) {
            this.context = context;

        }

        public void setList(List<SharedBean.DatasBean.DataBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return info == null ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return info == null ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_shared, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView3.setTag(position);
            SharedBean.DatasBean.DataBean bean=info.get(position);
            viewHolder.huiyuanname.setText(bean.getMember_name());
            viewHolder.happTime.setText(AbDateUtil.getStringByFormat(bean.getMember_time()*1000,"yyyy-MM-dd"));
            viewHolder.shopMoney.setText(bean.getYizhifu_tj_amount()+"");
            viewHolder.starMoney.setText(bean.getComplete_tj_amount()+"");
            final String id=bean.getMember_id();

            viewHolder.imageView3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
//        nextAct(ShraedDetilActivity.class);
        Intent intent = new Intent();
        intent.setClass(SharedActivity.this, ShraedDetilActivity.class);
        intent.putExtra("id", id);

        startActivity(intent);
    }
});
            return convertView;
        }



        class ViewHolder {
            @Bind(R.id.huiyuanname)
            TextView huiyuanname;
            @Bind(R.id.imageView3)
            ImageView imageView3;
            @Bind(R.id.happ_time)
            TextView happTime;
            @Bind(R.id.shop_money)
            TextView shopMoney;
            @Bind(R.id.star_money)
            TextView starMoney;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
